package com.sky.mapper;

import com.sky.annotation.AutoFill;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.enumeration.OperationType;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SetmealMapper {

    // 分页查询
    List<SetmealVO> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO);

    @Select("select * from setmeal where category_id = #{categoryId} and status = 1")
    List<SetmealVO> getByCategoryId(Integer categoryId);

    @Select("select sd.name,sd.copies,d.image,d.description "+
            "from setmeal_dish sd left join dish d on sd.dish_id = d.id "+
            "where sd.setmeal_id = #{setmealId}")
    List<DishItemVO> getDishItemBySetmealId(Long setmealId);

    @Insert("insert into setmeal (name,image,category_id,description,price,status,create_time,update_time,create_user,update_user) " +
            "values (#{name},#{image},#{categoryId},#{description},#{price},#{status},#{createTime},#{updateTime},#{createUser},#{updateUser})")
    @AutoFill(value = OperationType.INSERT)
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(Setmeal setmeal);

    @Insert("insert into setmeal_dish (setmeal_id,dish_id,name,price,copies) " +
            "values (#{setmealId},#{dishId},#{name},#{price},#{copies})")
    void insertSetmealDish(SetmealDish setmealDish);
    
    @Update("update setmeal set status = #{status} where id = #{id}")
    @AutoFill(value = OperationType.UPDATE)
    void updateStatus(Integer status, Long id);

    @Select("select * from setmeal where id = #{id}")
    Setmeal getById(Long id);
    
    @Delete("delete from setmeal where id = #{id}")
    void delete(Long id);

    @Delete("delete from setmeal_dish where setmeal_id = #{setmealId}")
    void deleteSetmealDish(Long setmealId);


    @Update("update setmeal set name = #{name}, image = #{image}, category_id = #{categoryId}, description = #{description}, price = #{price}, status = #{status}, update_time = #{updateTime}, update_user = #{updateUser} where id = #{id}")
    @AutoFill(value = OperationType.UPDATE)
    void update(Setmeal setmeal);

    @Select("select * from setmeal_dish where setmeal_id = #{id}")
    List<SetmealDish> getSetmealDishBySetmealId(Long id);
}
